package com.shop2cn.iapi.subscription.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.subscription.parameter.GetSubscriptionFuncStatusBean;
import com.shop2cn.iapi.subscription.service.GetSubscriptionFuncStatusCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
/**
*查询商家所有功能状态Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSubscriptionFuncStatus{
	private static GetSubscriptionFuncStatusBean getsubscriptionfuncstatusBean;
	private static GetSubscriptionFuncStatusCall getsubscriptionfuncstatusCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询商家所有功能状态Test");
	}

	@Before
	public void caseUp() {
		getsubscriptionfuncstatusBean=new GetSubscriptionFuncStatusBean();
		getsubscriptionfuncstatusCall=new GetSubscriptionFuncStatusCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsubscriptionfuncstatusCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubscriptionFuncStatus_001() {
		Logger.start(true,"正确数据，成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,1,2,3,4),true);
			
			GodmchManageCallService.ApplyCharge(mchId, 4, 5);
			getsubscriptionfuncstatusBean.setMchId(mchId);
			getsubscriptionfuncstatusCall.setData(getsubscriptionfuncstatusBean);
			getsubscriptionfuncstatusCall.callService();			
			Logger.verifyEquals("200", getsubscriptionfuncstatusCall.getString("code"), "验证code");
			equalsJson(Arrays.asList(4));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubscriptionFuncStatus_002() {
		Logger.start(true,"一年直播+一年线下，成功");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,1,2,3,4),true);
			
			GodmchManageCallService.ApplyCharge(mchId, 4, 5);
			GodmchManageCallService.ApplyCharge(mchId, 0, 3);
			getsubscriptionfuncstatusBean.setMchId(mchId);
			getsubscriptionfuncstatusCall.setData(getsubscriptionfuncstatusBean);
			getsubscriptionfuncstatusCall.callService();			
			Logger.verifyEquals("200", getsubscriptionfuncstatusCall.getString("code"), "验证code");
			equalsJson(Arrays.asList(0,4));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubscriptionFuncStatus_003() {
		Logger.start(true,"商户没有订购服务");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,1,2,3,4),true);
			
			getsubscriptionfuncstatusBean.setMchId(mchId);
			getsubscriptionfuncstatusCall.setData(getsubscriptionfuncstatusBean);
			getsubscriptionfuncstatusCall.callService();			
			Logger.verifyEquals("200", getsubscriptionfuncstatusCall.getString("code"), "验证code");
			equalsJson(Arrays.asList(999));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSubscriptionFuncStatus_004() {
		Logger.start(true,"商户设置了订购白名单，会自动订购服务");
		try {
			Integer mchId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			Integer DistributorId = GodmchManageCallService.CreateDistributor(mchId);
			GodmchManageCallService.SetTrueMchChargeConfig(mchId, Arrays.asList(0,1,2,3,4),true);
			
			getsubscriptionfuncstatusBean.setMchId(mchId);
			getsubscriptionfuncstatusCall.setData(getsubscriptionfuncstatusBean);
			getsubscriptionfuncstatusCall.callService();			
			Logger.verifyEquals("200", getsubscriptionfuncstatusCall.getString("code"), "验证code");
			equalsJson(Arrays.asList(999));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public void equalsJson(List<Integer> funcTypes){
		JsonObject jsonObject = getsubscriptionfuncstatusCall.getJsonObject();
		JsonArray asJsonArray = jsonObject.getAsJsonObject("data").getAsJsonArray("subscriptionFuncStatusList");
		asJsonArray.forEach(b->{
			if(funcTypes.contains(b.getAsJsonObject().get("funcType").getAsInt())){
				JsonObject asJsonObject = b.getAsJsonObject();
				Logger.verifyEquals(true, asJsonObject.get("available").getAsBoolean(), "验证available");
				Logger.verifyEquals(364, asJsonObject.get("availableDay").getAsInt(), "验证availableDay");
				Logger.verifyEquals(3, asJsonObject.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			}else {
				JsonObject asJsonObject = b.getAsJsonObject();
				Logger.verifyEquals(false, asJsonObject.get("available").getAsBoolean(), "验证available");
				Logger.verifyEquals(0, asJsonObject.get("availableDay").getAsInt(), "验证availableDay");
				Logger.verifyEquals(0, asJsonObject.get("subscriptionStatus").getAsInt(), "验证subscriptionStatus");
			}
		});
	}
	
	
	
}